package com.cyh.pangu.offer;

public class Offer21 {

    public int[] exchange(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        while (i < j) {
            if (i == nums.length) {
                break;
            }
            while (nums[i] % 2  == 1 && i < nums.length - 1) {
                i++;
            }
            while (nums[j] % 2 == 0 && j > 0)  {
                j--;
            }
            if (i >= j) {
                break;
            }
            int a = nums[i];
            nums[i] = nums[j];
            nums[j] = a;
            i++;
            j--;
        }
        return nums;
    }

    public int[] exchange_back(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] % 2 == 1 ) {
                continue;
            }

            boolean have =  true;
            for (int j = i+1; j < nums.length; j++) {
                if (nums[j] % 2 == 1 ) {
                    int a = nums[i];
                    nums[i] = nums[j];
                    nums[j] = a;
                    have = true;
                    break;
                }
                have = false;
            }
            if (!have) {
                break;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        int[] nums = {1,11,14};
        Offer21 offer21 = new Offer21();
        int[] result = offer21.exchange(nums);
        for (int n : result) {
            System.out.println("n:" + n);
        }
    }
}
